//
//  UILabel+Util.m
//  YDGJ
//
//  Created by ydcq on 16/5/30.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UILabel+Util.h"
#import <objc/runtime.h>

@interface UILabel ()

@property (nonatomic, copy) void (^tapBlk)(NSInteger);

@end

@implementation UILabel (Util)

@dynamic textBlk, textColorBlk;

+ (instancetype)labelWithFrame:(CGRect)rect text:(NSString *)str fontSize:(CGFloat)size textColor:(UIColor *)clr textAlignment:(NSTextAlignment)align
{
    return [self labelWithFrame:rect text:str fontSize:size textColor:clr textAlignment:align tapBlock:nil];
}

+ (instancetype)labelWithFrame:(CGRect)rect text:(NSString *)str fontSize:(CGFloat)size textColor:(UIColor *)clr textAlignment:(NSTextAlignment)align tapBlock:(void (^)(NSInteger))blk
{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.text = str;
    label.font = FontCustomSize(size);
    label.textColor = clr;
    label.textAlignment = align;
    
    if (blk) {
        objc_setAssociatedObject(label, "", blk, OBJC_ASSOCIATION_COPY_NONATOMIC);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:label action:@selector(tap)];
        label.userInteractionEnabled = YES;
        [label addGestureRecognizer:tap];
    }
    
    return label;
}

- (void)tap
{
    void (^blk)(NSInteger) = objc_getAssociatedObject(self, "");
    if (blk) {
        blk(self.tag);
    }
}

#pragma mark -getters

- (UILabel *(^)(NSString *))textBlk
{
    return ^(NSString *text){
        self.text = text;
        return self;
    };
}

- (UILabel *(^)(UIColor *))textColorBlk
{
    return ^(UIColor *clr){
        self.textColor = clr;
        return self;
    };
}

/**
 *  设置属性文字
 *
 *  @param textArr   需要显示的文字数组,如果有换行请在文字中添加 "\ n"换行符
 *  @param fontArr   字体数组, 如果fontArr与textArr个数不相同则获取字体数组中最后一个字体
 *  @param colorArr  颜色数组, 如果colorArr与textArr个数不相同则获取颜色数组中最后一个颜色
 *  @param spacing   换行的行间距
 *  @param alignment 换行的文字对齐方式
 */
- (void)setAttriStrWithTextArray:(NSArray<NSString *> *)textArr fontArr:(NSArray<UIFont *> *)fontArr colorArr:(NSArray<UIColor *> *)colorArr lineSpacing:(CGFloat)spacing alignment:(NSTextAlignment)alignment
{
    if (textArr.count >0 && fontArr.count >0 && colorArr.count > 0) {
        
        NSMutableString *allString = [NSMutableString string];
        for (NSString *tempText in textArr) {
            [allString appendFormat:@"%@",tempText];
        }
        
        NSRange lastTextRange = NSMakeRange(0, 0);
        NSMutableArray *rangeArr = [NSMutableArray array];
        
        for (NSString *tempText in textArr) {
            NSRange range = [allString rangeOfString:tempText];
            
            //如果存在相同字符,则换一种查找的方法
            if ([allString componentsSeparatedByString:tempText].count>2) { //存在多个相同字符
                range = NSMakeRange(lastTextRange.location+lastTextRange.length, tempText.length);
            }
            
            [rangeArr addObject:NSStringFromRange(range)];
            lastTextRange = range;
        }
        
        //设置属性文字
        NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:allString];
        for (int i=0; i<textArr.count; i++) {
            NSRange range = NSRangeFromString(rangeArr[i]);
            
            UIFont *font = (i > fontArr.count-1) ? [fontArr lastObject] : fontArr[i];
            [textAttr addAttribute:NSFontAttributeName value:font range:range];
            
            UIColor *color = (i > colorArr.count-1) ? [colorArr lastObject] : colorArr[i];
            [textAttr addAttribute:NSForegroundColorAttributeName value:color range:range];
        }
        
        //如果需要换行
        if ([allString rangeOfString:@"\n"].location != NSNotFound) {
            self.numberOfLines = 0;
        }
        
        self.attributedText = textAttr;
        
        //段落 <如果有换行 或者 字体宽度超过一行就设置行间距>
        if (self.width > Screen_Width || [allString rangeOfString:@"\n"].location != NSNotFound) {
            NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineSpacing = spacing;
            paragraphStyle.alignment = alignment;
            self.numberOfLines = 0;
            [textAttr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,allString.length)];
            self.attributedText = textAttr;
        }
        
    } else {
        self.text = @"文字,颜色,字体 每个数组至少有一个";
    }
}

@end
